package com.ymatou.shop.ui.view.item;

import java.util.Date;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.ymatou.shop.R;
import com.ymatou.shop.cache.ImageCache;
import com.ymatou.shop.model.Product;
import com.ymatou.shop.util.DateUtil;
import com.ymatou.shop.util.StringUtil;

public class OrderProductItemView extends FrameLayout {
	
	private ImageView mIconImageView;
	private TextView mDescriptionTextView;
	private TextView mDateTextView;
	private TextView mSaleNumTextView;
	
	private ImageCache.OnIconLoadCompleteListener mOnIconLoadCompleteListener;
	
	private Drawable mDefaultDrawable;

	public OrderProductItemView(Context context,
			ImageCache.OnIconLoadCompleteListener iconListener) {
		super(context);
		mOnIconLoadCompleteListener = iconListener;
		mDefaultDrawable = getResources().getDrawable(R.drawable.ic_launcher);
		
		initOrderProductItemView();
	}
	
	private void initOrderProductItemView() {
		LayoutInflater.from(getContext()).inflate(R.layout.list_order_product_item, this);
		
		mIconImageView = (ImageView) findViewById(R.id.icon_image);
		mDescriptionTextView = (TextView) findViewById(R.id.description_txt);
		mDateTextView = (TextView) findViewById(R.id.date_txt);
		mSaleNumTextView = (TextView) findViewById(R.id.sale_num_txt);
	}

	public void setData(Product product) {
		Drawable drawable = ImageCache.get(getContext(), product.mImage, mOnIconLoadCompleteListener);
		if (drawable == null) {
			mIconImageView.setImageDrawable(mDefaultDrawable);
		} else {
			mIconImageView.setImageDrawable(drawable);
		}
		
		mDescriptionTextView.setText(StringUtil.abbreviate(product.mDescription, 50));
		mDateTextView.setText(DateUtil.format(new Date(), "MM-dd HH:mm:ss"));/////////////////////////////
		mSaleNumTextView.setText(String.valueOf(product.mSaleNum));
	}
}
